import { createWebHashHistory, createRouter } from 'vue-router'
import routes from './routers'

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由挂载到window上，方便调试
window.$router = router

// 第一次启动 检查更新
let isFirstLaunch = import.meta.env.VITE_INIT_UPDATE_CHECK == '1'

router.beforeEach(async (to, from, next) => {
  if (isFirstLaunch) {
    next({ path: '/update', query: { isFirstLaunch } })
    isFirstLaunch = false
  } else {
    next()
  }
})

router.afterEach((to) => {
  // 设置网站标题
  if (to?.meta?.title) {
    document.title = to.meta.title
  }
})

export default router
